---
output:
  pdf_document: default
  html_document: default
---
```{r}
####
#### script prepared by Joshua Becker
#### contact info at 
#### www.joshua-becker.com
####
```

### Becker Porter Centola
### Wisdom of Partisan Crowds
### Experiment 2
### Main Text Results


```{r, echo=F, warning=F, message=F,results='hide'}
### SET YOUR WORKING DIRECTORY!!
rm(list=ls());gc()
library(dplyr)
if(file.exists("BeckerCentolaPorter_WisdomOfPartisanCrowds_Experiment2.Rdata")) {
  load("BeckerCentolaPorter_WisdomOfPartisanCrowds_Experiment2.Rdata")  
} else {
  source('Becker Centola Porter - Wisdom of Partisan Crowds - Exp 2 - Data Prep.R')
}

```
# RESULTS TOP SECTION

## Are the baseline differences in belief significant?  
```{r}
baseline = d %>% group_by(q) %>%
  summarize(
      wilcox.p.val = wilcox.test(  response_1[party=="Repub"]
                          , response_1[party=="Dem"])$p.val
  )

```
### Immigration Question
```{r}
baseline$wilcox.p.val[baseline$q=="Immigration"]
```
### Military Question
```{r}
baseline$wilcox.p.val[baseline$q=="Military"]
```
### Soldiers Quesiton
```{r}
baseline$wilcox.p.val[baseline$q=="Soldiers"]
```
### Unemployment Question
```{r}
baseline$wilcox.p.val[baseline$q=="Unemployment"]


### percen repubs/dems thinking unemployment went up/down
prop.table(table( d$response_1[d$q=="Unemployment"]<0
      ,d$party[d$q=="Unemployment"]), margin=2)

```

## Is the change in error for social conditions significant?

```{r}
control = subset(trial, network=="Control")
social = subset(trial, network=="Social")
repub_social = subset(trial, network=="Social" & party=="Repub")
dem_social = subset(trial, network=="Social" & party=="Dem")
repub_control = subset(trial, network=="Control" & party=="Repub")
dem_control = subset(trial, network=="Control" & party=="Dem")

```

### Republicans
```{r}
mean(repub_social$change_err_mean)
table(repub_social$change_err_mean<0)
wilcox.test(repub_social$change_err_mean)

mean(repub_control$change_err_mean)
wilcox.test(repub_control$change_err_mean)
```

### Democrats
```{r}
mean(dem_social$change_err_mean)
table(dem_social$change_err_mean<0)
wilcox.test(dem_social$change_err_mean)

mean(dem_control$change_err_mean)
wilcox.test(dem_control$change_err_mean)
```

## By what % did error change for social network conditions?
```{r}
mean(social$change_err_mean)/mean(social$err_mean_1)
```

## Did error of the mean change in the control condition?
```{r}
wilcox.test(control$change_err_mean)
```

## By what % did error change in control condition?
```{r}
mean(control$change_err_mean)/mean(control$err_mean_1)
```

## Was change in error significantly different between control and social?
```{r}
wilcox.test(  control$change_err_mean
            , social$change_err_mean)
```

## Did standard deviation decrease?

### Socal
```{r}
mean(social$change_sd)
wilcox.test(social$change_sd)
```

### Control
```{r}
mean(control$change_sd)
wilcox.test(control$change_sd)
```

### Compare social to control
```{r}
wilcox.test(social$change_sd
            , control$change_sd)

```

## Did individual error decrease?

### Social
```{r}
### Percent change
mean(social$change_err_ind)/mean(social$err_ind_1)

wilcox.test(social$change_err_ind)
```

### Control
```{r}
### Percent change
mean(control$change_err_ind)/mean(control$err_ind_1)


wilcox.test(control$change_err_ind)
```

### Compare Social and Control
```{r}
wilcox.test(social$change_err_ind
            , control$change_err_ind)
```

# RESULTS---SUBSECTION ON POLARIZATION
```{r}
social_paired = subset(trial_paired, network=="Social")
control_paired = subset(trial_paired, network=="Control")
```

## Examining change similarity of mean belief

### Change in similarity of mean belief for social condition
```{r}
### 11 out of 12 trials became more similar
table(social_paired$diff_mean_change<0)

mean(social_paired$diff_mean_change, na.rm=T) / mean(social_paired$diff_mean_1, na.rm=T)
wilcox.test(social_paired$diff_mean_change)
```

### Change in similarity of mean belief for control condition
```{r}
### All four control trials became LESS similar
control_paired$diff_mean_change<0

mean(control_paired$diff_mean_change) / mean(control_paired$diff_mean_1)
wilcox.test(control_paired$diff_mean_change)
```

### Compare change in similarity of mean for social and control
```{r}
wilcox.test(social_paired$diff_mean_change
            , control_paired$diff_mean_change)

```

## Examining change similarity of individual beliefs

### Change in pairwise similarity for social groups
```{r}
### All 12 trials became more similar
table(social_paired$pairwise_change<0)

### Percent change
mean(social_paired$pairwise_change, na.rm=T)/mean(social_paired$pairwise_1, na.rm=T)
wilcox.test(social_paired$pairwise_change)
```

### Change in pairwise similarity for control groups
```{r}
### ALl four trials became more similar
control_paired$pairwise_change<0

### Percent change
mean(control_paired$pairwise_change)/mean(control_paired$pairwise_1)
wilcox.test(control_paired$pairwise_change)
```

### Comparing social to control groups
```{r}
wilcox.test(  social_paired$pairwise_change
            , control_paired$pairwise_change)
```

### Did social groups with larger error make larger collective revisions?
```{r}
### For this question, we look at each individual task 
### instead of aggregating by trials.
### We add unique task intercepts to control for within-trial correlation.

social_questions = subset(aggreg, network=="Social")
summary(lm(change_err_mean ~ err_mean_1 + party + sub_trial, social_questions))
```


# Robustness Test with Median


## Did error of the median change in the control condition?
```{r}
mean(control$change_err_med)
mean(control$change_err_med<0)
wilcox.test(control$change_err_med)
```


## Did error of the median change in the social condition?
```{r}
mean(social$change_err_med)
mean(social$change_err_med<0)
wilcox.test(social$change_err_med)

  ### for each party separately, 
wilcox.test(social$change_err_med[social$party=="Dem"])
wilcox.test(social$change_err_med[social$party=="Repub"])
```


## Was change in error significantly different between control and social?
```{r}
wilcox.test(  control$change_err_med
            , social$change_err_med)
```
